﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
namespace LotteryAdmin
{
    public partial class LotteryStatus : UserControl
    {
        private LotteryAdminClient srv;
        private int currentPos = 0;
        private string  currentEditionName;
        bool isNameClick = false;
        public LotteryStatus()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LotteryStatus_Loaded);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        void LotteryStatus_Loaded(object sender, RoutedEventArgs e)
        {
            LoadLottery();
        }
        void LoadLottery()
        {
            currentPos = 0;
            this.tabItem0.IsSelected = true;
            this.sp_playradio.Visibility = Visibility.Collapsed;
            this.sp_playtype.Visibility = Visibility.Collapsed;
            this.sp_lottery.Visibility = Visibility.Visible;
            srv = WcfProxy.GetProxy;
            loadingProgress.IsBusy = true;
            srv.GetLotteryInfoAsync(txtLotteryType.Text.Trim(),
                (cbxIsEnable.SelectedItem as ComboBoxItem).Tag.ToString(),
                (cbxOrientation.SelectedItem as ComboBoxItem).Tag.ToString());
            srv.GetLotteryInfoCompleted += (s, er) =>
                {
                    loadingProgress.IsBusy = false;
                    this.dgLottery.ItemsSource = er.Result;
                };
        }
        void LoadPlayType()
        {
            currentPos = 0;
            this.tabItem1.IsSelected = true;
            this.sp_playradio.Visibility = Visibility.Collapsed;
            this.sp_playtype.Visibility = Visibility.Visible;
            this.sp_lottery.Visibility = Visibility.Visible;
            srv = WcfProxy.GetProxy;
            loadingProgress.IsBusy = true;
            srv.GetPlayTypeInfoAsync(txtLotteryType.Text.Trim(),
                (cbxIsEnable.SelectedItem as ComboBoxItem).Tag.ToString(),
                (cbxOrientation.SelectedItem as ComboBoxItem).Tag.ToString(),
                txtPlayType.Text.Trim());
            srv.GetPlayTypeInfoCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayType.ItemsSource = er.Result;
            };
        }
        void LoadPlayType(LotteryInfo l)
        {
            currentPos = 0;
            this.tabItem1.IsSelected = true;
            this.sp_playradio.Visibility = Visibility.Collapsed;
            this.sp_playtype.Visibility = Visibility.Visible;
            this.sp_lottery.Visibility = Visibility.Visible;
            srv = WcfProxy.GetProxy;
            srv.GetPlayTypeInfoByLotteryIdAsync(l.LotteryTypeID);
            loadingProgress.IsBusy = true;
            srv.GetPlayTypeInfoByLotteryIdCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayType.ItemsSource = er.Result;
            };
        }
        void LoadPlayRadio()
        {
            currentPos = 0;
            this.tabItem2.IsSelected = true;
            this.sp_playradio.Visibility = Visibility.Visible;
            this.sp_playtype.Visibility = Visibility.Visible;
            this.sp_lottery.Visibility = Visibility.Visible;
            srv = WcfProxy.GetProxy;
            srv.GetPlayRadioInfoAsync(txtLotteryType.Text.Trim(),
                (cbxIsEnable.SelectedItem as ComboBoxItem).Tag.ToString(),
                (cbxOrientation.SelectedItem as ComboBoxItem).Tag.ToString(),
                txtPlayType.Text.Trim(), txtPlayRadio.Text.Trim());
            loadingProgress.IsBusy = true;
            srv.GetPlayRadioInfoCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayRadio.ItemsSource = er.Result;
            };
        }
        void LoadPlayRadio(PlayTypeInfo l)
        {
            currentPos = 0;
            this.tabItem2.IsSelected = true;
            this.sp_playradio.Visibility = Visibility.Visible;
            this.sp_playtype.Visibility = Visibility.Visible;
            this.sp_lottery.Visibility = Visibility.Visible;
            srv = WcfProxy.GetProxy;
            srv.GetPlayRadioInfoByPlayTyPeIdAsync(l.PlayTypeID);
            loadingProgress.IsBusy = true;
            srv.GetPlayRadioInfoByPlayTyPeIdCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayRadio.ItemsSource = er.Result;
            };
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateName(this.txtLotteryType.Text))
            {
                MessageBox.Show("彩种名称只能输入中文、英文、数字、下划线");
                return;
            }
            if (!Validate.ValidateName(this.txtPlayType.Text))
            {
                MessageBox.Show("玩法名称只能输入中文、英文、数字、下划线");
                return;
            }
            if (!Validate.ValidateName(this.txtPlayRadio.Text))
            {
                MessageBox.Show("玩法单选名称只能输入中文、英文、数字、下划线");
                return;
            }
            if (this.tabItem1.IsSelected)
                LoadPlayType();
            if (this.tabItem0.IsSelected)
                LoadLottery();
            if (this.tabItem2.IsSelected)
                LoadPlayRadio();

        }

        private void lotteryName_Click(object sender, MouseButtonEventArgs e)
        {
            isNameClick = true;
            LoadPlayType(((TextBlock)sender).Tag as LotteryInfo);
            isNameClick = false;
        }
        private void playTypeName_Click(object sender, MouseButtonEventArgs e)
        {
            isNameClick = true;
            LoadPlayRadio(((TextBlock)sender).Tag as PlayTypeInfo);
            isNameClick = false;
        }


        private void dgLottery_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgLottery.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex()+1).ToString();
            (this.dgLottery.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            (this.dgLottery.Columns[3].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            (this.dgLottery.Columns[4].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            LotteryInfo lottery = e.Row.DataContext as LotteryInfo;
            if (lottery.Orientation == 1)
                (this.dgLottery.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgLottery.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Green);
            if (lottery.IsEnable == 1)
                (this.dgLottery.Columns[3].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgLottery.Columns[3].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
            if (lottery.IsOpen == 1)
                (this.dgLottery.Columns[4].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgLottery.Columns[4].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
        }
        private void dgLottery_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
        {
            LotteryInfo lottery = e.Row.DataContext as LotteryInfo;
            if (lottery.LotteryName == currentEditionName)
            {
                (this.dgLottery.Columns[3].GetCellContent(e.Row) as Button).Tag = lottery;
                (this.dgLottery.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                (this.dgLottery.Columns[1].GetCellContent(e.Row) as TextBlock).Text = currentEditionName;
                return;
            }
            srv = WcfProxy.GetProxy;
            srv.UpdateLotteryNameCompleted += (s, er) =>
                {
                    loadingProgress.IsBusy = false;
                    if (er.Result)
                    {
                        (this.dgLottery.Columns[3].GetCellContent(e.Row) as Button).Tag = lottery;
                        (this.dgLottery.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                        (this.dgLottery.Columns[1].GetCellContent(e.Row) as TextBlock).Text = lottery.LotteryName;
                    }
                };
            loadingProgress.IsBusy = true;
            srv.UpdateLotteryNameAsync(lottery);
        }
        private void LotteryButton_Click(object sender, RoutedEventArgs e)
        {
             LotteryInfo lottery = (sender as Button).Tag as LotteryInfo;
             if (lottery.IsEnable == 0)
                 lottery.IsEnable = 1;
             else
                 lottery.IsEnable = 0;
             srv = WcfProxy.GetProxy;
             srv.UpdateLotteryIsEnableCompleted += (s, er) =>
             {
                 loadingProgress.IsBusy = false;
                 if (er.Result)
                 {
                     (sender as Button).Tag = lottery;
                     if (lottery.IsEnable == 0)
                         (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                     else
                         (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                 }
             };
             srv.UpdateLotteryIsEnableAsync(lottery);
             loadingProgress.IsBusy = true;
        }
        private void dgLottery_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            currentEditionName = (e.Row.DataContext as LotteryInfo).LotteryName;
        }
      
        private void dgPlayType_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
        {
            PlayTypeInfo lottery = e.Row.DataContext as PlayTypeInfo;
            if (lottery.PlayTypeName == currentEditionName)
            {
                (this.dgPlayType.Columns[3].GetCellContent(e.Row) as TextBlock).Text = currentEditionName;
                (this.dgPlayType.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                (this.dgPlayType.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                (this.dgPlayType.Columns[4].GetCellContent(e.Row) as Button).Tag = lottery;
                return;
            }
            srv = WcfProxy.GetProxy;
            srv.UpdatePlayTypeNameCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (this.dgPlayType.Columns[3].GetCellContent(e.Row) as TextBlock).Text = lottery.PlayTypeName;
                    (this.dgPlayType.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                    (this.dgPlayType.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                    (this.dgPlayType.Columns[4].GetCellContent(e.Row) as Button).Tag = lottery;
                }
            };
            srv.UpdatePlayTypeNameAsync(lottery);
            loadingProgress.IsBusy = true;
        }
        private void dgPlayType_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgPlayType.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex()+1).ToString();
            (this.dgPlayType.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            (this.dgPlayType.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            (this.dgPlayType.Columns[4].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            PlayTypeInfo lottery = e.Row.DataContext as PlayTypeInfo;
            if (lottery.Orientation == 1)
                (this.dgPlayType.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgPlayType.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Green);
            if (lottery.IsEnable == 1)
                (this.dgPlayType.Columns[4].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgPlayType.Columns[4].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
        }
        private void PlayTypeButton_Click(object sender, RoutedEventArgs e)
        {
            PlayTypeInfo lottery = (sender as Button).Tag as PlayTypeInfo;
            if (lottery.IsEnable == 0)
                lottery.IsEnable = 1;
            else
                lottery.IsEnable = 0;
            srv = WcfProxy.GetProxy;
            srv.UpdatePlayTypeIsEnableCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (sender as Button).Tag = lottery;
                    if (lottery.IsEnable == 0)
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                    else
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                }
            };
            srv.UpdatePlayTypeIsEnableAsync(lottery);
            loadingProgress.IsBusy = true;
        }
        private void dgPlayType_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            currentEditionName = (e.Row.DataContext as PlayTypeInfo).PlayTypeName;
        }

        private void dgPlayRadio_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
        {
            PlayTypeRadioInfo lottery = e.Row.DataContext as PlayTypeRadioInfo;
            if (lottery.PlayTypeRadioName == currentEditionName)
            {
                (this.dgPlayRadio.Columns[4].GetCellContent(e.Row) as TextBlock).Text = currentEditionName;
                (this.dgPlayRadio.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                (this.dgPlayRadio.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                (this.dgPlayRadio.Columns[5].GetCellContent(e.Row) as Button).Tag = lottery;
                return;
            }
            srv = WcfProxy.GetProxy;
            srv.UpdatePlayRadioNameCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (this.dgPlayRadio.Columns[4].GetCellContent(e.Row) as TextBlock).Text = lottery.PlayTypeRadioName;
                    (this.dgPlayRadio.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                    (this.dgPlayRadio.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = lottery;
                    (this.dgPlayRadio.Columns[5].GetCellContent(e.Row) as Button).Tag = lottery;
                }
            };
            srv.UpdatePlayRadioNameAsync(lottery);
            loadingProgress.IsBusy = true;
        }
        private void dgPlayRadio_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            currentEditionName = (e.Row.DataContext as PlayTypeRadioInfo).PlayTypeRadioName;
        }
        private void dgPlayRadio_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgPlayRadio.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex() + 1).ToString();
            (this.dgPlayRadio.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            (this.dgPlayRadio.Columns[3].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            (this.dgPlayRadio.Columns[5].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            PlayTypeRadioInfo lottery = e.Row.DataContext as PlayTypeRadioInfo;
            if (lottery.Orientation == 1)
                (this.dgPlayRadio.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgPlayRadio.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Green);
            if (lottery.IsEnable == 1)
                (this.dgPlayRadio.Columns[5].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            else
                (this.dgPlayRadio.Columns[5].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
        }
        private void PlayRadioButton_Click(object sender, RoutedEventArgs e)
        {
            PlayTypeRadioInfo lottery = (sender as Button).Tag as PlayTypeRadioInfo;
            if (lottery.IsEnable == 0)
                lottery.IsEnable = 1;
            else
                lottery.IsEnable = 0;
            srv = WcfProxy.GetProxy;
            srv.UpdatePlayRadioIsEnableCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (sender as Button).Tag = lottery;
                    if (lottery.IsEnable == 0)
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                    else
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                }
            };
            srv.UpdatePlayRadioIsEnableAsync(lottery);
            loadingProgress.IsBusy = true;
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isNameClick)
                return;
            if ((sender as TabControl).SelectedItem == tabItem0)
                LoadLottery();
            if ((sender as TabControl).SelectedItem == tabItem1)
                LoadPlayType();
            if ((sender as TabControl).SelectedItem == tabItem2)
                LoadPlayRadio();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LotteryInfo lottery = (sender as Button).Tag as LotteryInfo;
            if (lottery.IsOpen == 0)
                lottery.IsOpen = 1;
            else
                lottery.IsOpen = 0;
            srv = WcfProxy.GetProxy;
            srv.UpdateLotteryIsOpenCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (sender as Button).Tag = lottery;
                    if (lottery.IsOpen == 0)
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                    else
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                }
            };
            srv.UpdateLotteryIsOpenAsync(lottery);
            loadingProgress.IsBusy = true;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            LotteryAdd sb = new LotteryAdd();
            sb.Show();
            sb.Closed += (s, er) =>
            {
                if (sb.DialogResult == true)
                    LoadLottery();
            };
        }



    }
}
